package com.lijin.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * Created by abbott-jx on 2017/11/8.
 */

@Configuration //添加这个注解的class，只在容器启动的时候加载一次

@EnableAsync //启用异步任务：TaskExecutor

@EnableScheduling //启用定时任务：TaskScheduler

public class ScheduledConfigure {

  @Bean("asyncExecutor") //指定TaskExecutor 的bean name

  public TaskExecutor taskExecutor() {

    ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();

    threadPoolTaskExecutor.setCorePoolSize(10);

    threadPoolTaskExecutor.setMaxPoolSize(20);

    threadPoolTaskExecutor.setQueueCapacity(1000);

    threadPoolTaskExecutor.setThreadNamePrefix("async-t-");

    return threadPoolTaskExecutor;

  }

 @Bean
  public TaskScheduler taskScheduler() {

    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();

    scheduler.setThreadNamePrefix("p-scheduler-");

    scheduler.setPoolSize(10);

    return scheduler;

 }


}
